<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <!--<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index214.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }


    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" type="success" icon="el-icon-search" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" icon="el-icon-plus" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" stripe="true" row-key="id" expand-on-click-node="true" :data="dataList"
                      :indent="100" :select-on-indeterminate="true" stripe highlight-current-row
                      style="width: 100%;margin-bottom: 20px;"
                      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column prop="id" label="菜单id" align="center"></el-table-column>
                <el-table-column prop="name" label="菜单名称" width="300x" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="菜单地址" align="center"></el-table-column>
                <el-table-column prop="icon" label="菜单图标" align="center">
                    <template slot-scope="scope">
                        <i class="fa" :class="scope.row.icon"></i>
                    </template>
                </el-table-column>
                <el-table-column label="菜单等级" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.level == '1' ? '一级菜单' : scope.row.level == '2' ? '二级菜单' :  '三级菜单'}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-close" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>



            <!-- 新增弹窗 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form   ref="dataAddForm" label-position="right" label-width="100px"  :rules="rule1" :model="formData">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name" />
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单地址" >
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="所属菜单"  >
                                                <el-select v-model="formData.id" >
                                                    <el-option label="创建一级菜单" value="0"></el-option>
                                                    <el-option :label="item.name" :value="item.id" v-for="item in menus">
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单图标">
                                                <el-select v-model="formData.icon" >
                                                    <el-option :label="item.icon" :value="item.icon" v-for="item in icons">
                                                        <i class="fa" :class="item.icon"></i>
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑弹窗 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" label-position="right" label-width="100px" :rules="rule2" :model="formData" >
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单地址">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="所属菜单">
                                                <el-select v-model="formData.id" >
                                                    <el-option label="无(一级菜单)" value="0"></el-option>
                                                    <el-option :label="item.name" :value="item.id" v-for="item in menus">
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单图标">
                                                <el-select v-model="formData.icon" >
                                                    <el-option :label="item.icon" :value="item.icon" v-for="item in icons">
                                                        <i class="fa" :class="item.icon"></i>
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    new Vue({
        el: '#app',
        data: function() {
            var checkEditName = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('菜单名称为必填项'));
                }

                var params ={name:value,
                    id:this.menuid};
                axios.post("/menu/checkName.do",params).then(res=> {
                    if (res.data){
                        callback(new Error('菜单名称重复'));
                    }else {
                        callback();
                    }
                });
            };

            var checkAddName = (rule, value, callback) => {
                this.menuid=null;
                if (!value) {
                    return callback(new Error('菜单名称为必填项'));
                }
                var params ={name:value,
                    id:this.menuid};
                axios.post("/menu/checkName.do",params).then(res=> {
                    if (res.data){
                        callback(new Error('菜单名称重复'));
                    }else {
                        callback();
                    }
                });
            };
            return{
                autoUpload: true,//自动上传
                activeName:'first',//添加/编辑窗口Tab标签名称
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize:10,
                    total:6,
                    queryString:null,
                },
                //新增菜单回显一级菜单数据
                menus:[
                    //id: name:
                ],
                //新增菜单图标展示数据
                icons:[
                    {icon:"fa-bomb"},
                    {icon:"fa-battery-full"},
                    {icon:"fa-bell-o"},
                    {icon:"fa-thermometer-full"},
                    {icon:"fa-commenting-o"},
                    {icon:"fa-envelope"},
                    {icon:"fa-thumbs-o-up"},
                    {icon:"fa-wheelchair"},
                    {icon:"fa-apple"},
                    {icon:"fa-slideshare"},
                    {icon:"fa-android"}
                ],
                dataList: [],//列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
                  rule1: {//新增菜单名校验规则
                    name: [{required: true , validator: checkAddName , trigger: 'blur' }],
                },rule2: {//编辑菜单名校验规则
                    name: [{ required: true , validator: checkEditName , trigger: 'blur' }],
                },
                //用于菜单重复名的校验
                menuid:null

            }
        },

        created() {
            this.findPage();
        },
        methods: {
            //查询菜单
            findPage(){
                axios.post("/menu/getMenuList.do",this.pagination).then(response=>{
                    if(response.data.flag){
                        this.dataList=response.data.data;
                    }else{
                        this.$message.error(response.data.message)
                    }
                });
            },
            //新增确定
            handleAdd(){
                 //校验
                this.$refs["dataAddForm"].validate((valid) => {
                    if (valid) {
                        axios.post("/menu/add.do",this.formData).then(response=>{
                            //关闭弹窗
                            this.dialogFormVisible=false;
                            if(response.data.flag){
                                this.$message.success(response.data.message)
                            }else{
                                this.$message.error(response.data.message)
                            }
                            this.findPage();
                        })
                    }
                });
            },
            //编辑确定
            handleEdit(){
                //校验
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        axios.post("/menu/edit.do",this.formData).then(response=>{
                            //关闭弹窗
                            this.dialogFormVisible4Edit=false;
                            //判断
                            if(response.data.flag){
                                this.$message.success(response.data.message);
                            }else{
                                this.$message.error(response.data.message);
                            }
                            this.findPage();
                        });
                    }
                });
            },
            //弹出编辑窗口
            handleUpdate(row) {
                //重置校验信息
                this.$nextTick(() => {
                    this.$refs.dataEditForm.resetFields()
                });
                this.dialogFormVisible4Edit = true;
                //设置校验菜单id
                this.menuid=row.id;
                //回显数据
                axios.get("/menu/getMenu.do?menuId="+row.id).then(response=>{
                  if(response.data.flag){
                      this.formData=response.data.data
                  }else{
                      this.$message.error(response.data.message)
                  }
                  //回显所属菜单数据
                  this.listLevel1Menu();
              });
            },

            // 重置表单
            resetForm() {
                this.formData = {};
                this.tableData = [];
                this.activeName= "first";
            },
            // 弹出添加窗口
            handleCreate() {
                //重置校验信息
                this.$nextTick(() => {
                    this.$refs.dataAddForm.resetFields()
                });
                this.dialogFormVisible = true;
                //1.重置表单
                this.resetForm();
                //2.回显一级菜单数据
                this.listLevel1Menu();
            },
            //回显一级菜单数据
            listLevel1Menu(){
                axios.get("/menu/getLevel1Menu.do").then(response=>{
                    if(response.data.flag){
                        this.menus=response.data.data;
                    }else{
                        this.$message.error(response.data.message)
                    }
                })
            }
            ,
            //删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除菜单记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送删除请求
                    axios.get("/menu/deleteById.do?menuId="+row.id).then(res=>{
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                        this.findPage();//分页查询
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }

    })
</script>
</html>
